//
//  MLElementButtonBroadcastCell.m
//  CustomerPo
//
//  Created by molon on 8/12/14.
//  Copyright (c) 2014 molon. All rights reserved.
//

#import "MLElementButtonBroadcastCell.h"
#import "MLElementButtonView.h"
#import "MLElementButtonItem.h"
#import "UIView+MLKit.h"

@interface MLElementButtonBroadcastCell()

@property (nonatomic,strong) NSMutableArray *elementViews;

@end

@implementation MLElementButtonBroadcastCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - reuse
- (void)prepareForReuse
{
    [super prepareForReuse];
    //重用需要做的
    self.items = nil;
}

#pragma mark - getter
- (NSMutableArray *)elementViews
{
	if (!_elementViews) {
		_elementViews = [NSMutableArray array];
	}
	return _elementViews;
}


#pragma mark - setter
- (void)setItems:(NSArray *)items
{
    _items = items;
    if (!items) {
        //移除所有的子View
        for (UIView *subView in self.subviews) {
            [subView removeFromSuperview];
        }
        return;
    }
    
    [self.elementViews removeAllObjects];
    for (NSUInteger i=0; i<items.count; i++) {
        MLElementButtonView *elementView = [MLElementButtonView new];
        elementView.elementItem = items[i];
        [self addSubview:elementView];
        [self.elementViews addObject:elementView];
    }
    [self setNeedsLayout];
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    MLElementButtonItemCombination *protypeCombination = ((MLElementButtonItem*)[self.items firstObject]).combination;
    
    CGFloat elementXSpacing = protypeCombination.elementXSpacing;
    CGFloat elementYSpacing = protypeCombination.elementYSpacing;
    CGFloat elementWidth = MIN(self.frameWidth, protypeCombination.elementWidth);
    CGFloat elementHeight = MIN(self.frameHeight, protypeCombination.elementHeight);
    
    CGFloat elementOccupyWidth = elementWidth+elementXSpacing*2;
    CGFloat elementOccupyHeight = elementHeight+elementYSpacing*2;
    
    //计算出每行最多可以放几个
    NSInteger maxElementCountOfRow = MAX(1, floor((self.frameWidth-elementXSpacing*2)/elementOccupyWidth));
    //计算出这个高度可以放置多少行
    NSInteger maxRowCountOfCell = MAX(1, floor((self.frameHeight-elementYSpacing*2)/elementOccupyHeight));
    
    if (maxElementCountOfRow==1) {
        elementOccupyWidth = self.frameWidth;
        elementXSpacing = (self.frameWidth-elementWidth)/2;
    }
    if (maxRowCountOfCell==1) {
        elementOccupyHeight = self.frameHeight;
        elementYSpacing = (self.frameHeight-elementHeight)/2;
    }
    
    //修正cellXPadding
    CGFloat cellXPadding = (self.frameWidth-maxElementCountOfRow*elementOccupyWidth)/2;
    CGFloat cellYPadding = (self.frameHeight-maxRowCountOfCell*elementOccupyHeight)/2;
    
    CGFloat baseY = cellYPadding+elementYSpacing;
    CGFloat baseX = cellXPadding+elementXSpacing;
    for (NSUInteger i=0; i<self.elementViews.count; i++) {
        UIView *elementView = self.elementViews[i];
        elementView.frame = CGRectMake(baseX, baseY, elementWidth, elementHeight);
        if ((i+1)%maxElementCountOfRow==0) {
            baseX = cellXPadding+elementXSpacing;
            baseY = elementView.frameBottom+elementYSpacing*2;
        }else{
            baseX = elementView.frameRight+elementXSpacing*2;
        }
    }
}



@end
